//
//  TPPickerViewController.m
//  
//
//  Created by leonardo on 27/03/2017.
//
//

#import "TPPickerViewController.h"
#import "TPUIKit.h"
@interface TPPickerViewController ()



@property (nonatomic , strong) UIPickerView * pickerView;


@property (nonatomic , strong) UIView * headerContainer;
@property (nonatomic , strong) UIButton * btn_done;
@property (nonatomic , strong) UILabel * label_tip;
@end

@implementation TPPickerViewController
- (instancetype)initWithTip:(NSString *) tip
{
    self = [super init];
    if (self) {
        self.modalPresentationStyle = UIModalPresentationCustom;
        _btn_done = [UIButton buttonWithType:UIButtonTypeSystem];
        _pickerView = [[UIPickerView alloc]init];
        _label_tip = [UILabel new];
        _tip = tip;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = COLOUR_TRANSLUCENT;
    _headerContainer = [UIView new];
    [self.view addSubview:_headerContainer];
    
        [_btn_done setTitle:@"Done" forState:UIControlStateNormal];
        [_headerContainer addSubview:_btn_done];
        
    
        _label_tip.text = _tip;
        _label_tip.textColor = COLOUR_GRAY;
        [_headerContainer addSubview:_label_tip];
    
    _pickerView.backgroundColor = COLOUR_WHITE;
     _pickerView.delegate = self.pickerViewDelegate;
     _pickerView.dataSource = self.pickerViewDataSource;
    [self.view addSubview:_pickerView];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [_headerContainer mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(_headerContainer.superview);
        make.height.mas_equalTo(HEIGHT_BTN_NORMAL);
    }];
    [_btn_done mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(_btn_done.superview);
        make.top.bottom.mas_equalTo(_btn_done.superview);
    }];
    [_label_tip mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.mas_equalTo(_label_tip.superview);
        make.center.mas_equalTo(_label_tip.superview);
    }];
    [_pickerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_headerContainer.mas_bottom);
        make.left.right.bottom.mas_equalTo(_pickerView.superview);
        make.height.mas_equalTo(200);
    }];
    
    
    
}
-(void)viewDidAppear:(BOOL)animated{
    [super viewDidDisappear:animated];
    UITapGestureRecognizer * tapGR = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(bgViewTap)];
    [self.view addGestureRecognizer:tapGR];
    
    [_btn_done addTarget:self action:@selector(btn_doneTap) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark - public
-(void)reloadAllComponents{
    [_pickerView reloadAllComponents];
}

-(void)reloadComponent:(NSInteger) numberOfComponent{
    [_pickerView reloadComponent:numberOfComponent];
}

#pragma mark - private

-(void)bgViewTap{
    [self dismiss];
}
-(void)btn_doneTap{
    [self dismiss];
}
-(void)dismiss{
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//-(void)setPickerViewDelegate:(id<UIPickerViewDelegate>)pickerViewDelegate{
//    _pickerView.delegate = pickerViewDelegate;
//}
//-(void)setPickerViewDataSource:(id<UIPickerViewDataSource>)pickerViewDataSource{
//    _pickerView.dataSource = pickerViewDataSource;
//}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
